local S = require "syscall"


    local pid0 = S.getppid()
    print("parent pid = ", pid0)
    local pid = S.fork()
    if pid == 0 then -- 生成守护进程
    	worker_pid = S.fork() --生成工作进程
    	if worker_pid == 0 then
        	print("daemon worker_pid= " , S.getpid())
        	i = 1
        	while true do
                S.sleep(50)
        	--util.writefile("1.log", "d" .. tostring(i), "RWXU") --http://www.masella.name/~andre/vapis/libgit2/Git.FileMode.html
        	i = i + 1
        	end
        else
            print("daemon pid= " , S.getpid())
            for sig in ipairs({1,2,3,15,9}) do --http://ph7spot.com/musings/introduction-to-unix-signals-and-system-calls
                S.signal(sig, function() S.kill(worker_pid, 15);S.exit(0) end)
            end

            --S.sigaction(9, S.kill(0, 15)) --KILL
            --S.sigaction(1, S.exit(0)) --HUP
            --S.sigaction(2, S.exit(0)) --ctrl+c
            --S.sigaction(3, S.exit(0)) --QUIT
            --S.sigaction(15, S.exit(0)) --TERM
            while true do
                local w = S.wait() --wait for child process
                if w and not w.EXITSTATUS then --when child exit ,fork new child
                --print("w.pid=", w.pid, w.WTERMSIG, w.EXITSTATUS, w.WIFEXITED)
                S.fork() 
                end
            end

    	end
      --S.exit(23)
    else -- parent
      --local w = assert(S.wait())
      --S.signal(2, function () print("receive signal 9")  end) 
      print("run pid=", S.getpid())
      --S.exit(23)
    end
